﻿using EmailHelper.Model;
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EmailHelper.Exchange
{
    /// <summary>
    /// EWS发送帮助类
    /// </summary>
    public class EWSSendHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="message">待发送的邮件信息</param>
        /// <param name="IsSendSave">发送后是否保存到已发送邮件列表</param>
        /// <returns></returns>
        public static async Task<SendResultEntity> SendEmail(EmailMessage message, bool IsSendSave = true)
        {
            SendResultEntity sendResultEntity = new SendResultEntity();
            try
            {
                if (IsSendSave)
                {
                    await message.SendAndSaveCopy();
                }
                else
                {
                    await message.Send();
                }
            }
            catch (Exception ex)
            {
                sendResultEntity.ResultInformation = $"邮件发送失败:{ex.Message}";
                sendResultEntity.ResultStatus = false;
            }
            return sendResultEntity;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="service"></param>
        /// <param name="email"></param>
        /// <param name="IsSendSave"></param>
        /// <returns></returns>
        public async static Task<SendResultEntity> SendMail(ExchangeService service, EmailInfoExchange email, bool IsSendSave = true)
        {
            SendResultEntity sendResultEntity = new SendResultEntity();
            try
            {
                //发送人
                Mailbox mail = new Mailbox(email.Mail_from);
                //邮件内容
                EmailMessage message = new EmailMessage(service);
                string[] strTos = email.Mail_to.Split(';');
                //接收人
                foreach (string item in strTos)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        message.ToRecipients.Add(item);
                    }
                }
                //抄送人
                foreach (string item in email.Mail_cc.Split(';'))
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        message.CcRecipients.Add(item);
                    }
                }
                //邮件标题
                message.Subject = email.Subject;
                //邮件内容
                //message.Body = new MessageBody(email.body);
                if (IsSendSave)
                {
                    await message.SendAndSaveCopy();  //发送并且保存
                }
                else
                {
                    await message.Send();
                }
            }
            catch (Exception ex)
            {
                sendResultEntity.ResultInformation = $"邮件发送失败:{ex.Message}";
                sendResultEntity.ResultStatus = false;
            }
            return sendResultEntity;
        }

    }
}
